<?php

include_once 'common.php';

$subjects = json_stringify($payload->subjects);
$updateTime = date('Y-m-d H:i:s');
$sql = '';
if ($payload->number) {
  // update
  $sql = "UPDATE `trex_exercises` SET `title`='{$payload->title}',`year`={$payload->year},`cycle`={$payload->cycle},`type`={$payload->type},`office`='{$payload->office}',`subjects`='{$subjects}',`hours`={$payload->hours},`description`='{$payload->description}',`startDate`='{$payload->startDate}',`endDate`='{$payload->endDate}',`author`='{$payload->author}',`updateTime`='{$updateTime}',`valid`={$payload->valid},`disposed`={$payload->disposed} WHERE number = {$payload->number}";
} else {
  // insert
  $sql = "INSERT INTO `trex_exercises`(`number`, `title`, `year`, `cycle`, `type`, `office`, `subjects`, `hours`, `description`, `startDate`, `endDate`, `author`, `updateTime`, `valid`, `disposed`) VALUES (null, '{$payload->title}', {$payload->year}, {$payload->cycle}, {$payload->type}, '{$payload->office}', '{$subjects}', {$payload->hours}, '{$payload->description}', '{$payload->startDate}', '{$payload->endDate}', '{$payload->author}', '{$updateTime}', {$payload->valid}, {$payload->disposed})";
}

$result = exec_sql($sql);

output(['sql' => $sql, 'result' => $result, 'payload' => $payload]);